package com.example.composableexample.animate

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.TopAppBar
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp


@Composable
fun AnimatedVisibilitySimple() {
    val state = MutableTransitionState(false)
    Column {
        AnimatedVisibility(visibleState = state) {
            Text(text = "Compose")
        }
        Button(onClick = { state.targetState = !state.targetState }) {
            Text(text = "点击")
            if (state.isIdle && state.targetState) {
                Text(text = "动画结束")
            }
        }

    }
}


@Composable
fun Sort() {
    var shown by remember { mutableStateOf(false) }

    Column(modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally) {
        Button(onClick = { shown = !shown }) {
            Text(text = "排序")
        }
        AnimatedVisibility(
            visible = shown,
            enter = slideInVertically(
                initialOffsetY = { fullHeight -> -fullHeight },
                // tween() 基于时长的AnimationSpec
                animationSpec = tween(durationMillis = 150, easing = LinearOutSlowInEasing)
            ),
            exit = slideOutVertically(
                targetOffsetY = { fullHeight -> -fullHeight },
                animationSpec = tween(durationMillis = 250, easing = FastOutLinearInEasing)
            )

        ) {
            Surface(
                modifier = Modifier.fillMaxWidth(),
                color = MaterialTheme.colors.secondary,
                elevation = 4.dp
            ) {
                Text(
                    text = "综合排序",
                    modifier = Modifier
                        .padding(16.dp)
                        .height(100.dp)
                        .clickable { shown = false }
                )
            }
        }
    }

}

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun AnimateEnterExitSimple() {
    var visible by remember {
        mutableStateOf(false)
    }
    Column(modifier = Modifier.fillMaxSize()) {
        Button(onClick = { visible = !visible }) {
            Text(text = "click")
        }
        AnimatedVisibility(visible = visible, enter = fadeIn(), exit = fadeOut()) {
            Box(modifier = Modifier
                .fillMaxSize()
                .background(color = Color.DarkGray)
            ) {
                Box(modifier = Modifier
                    .align(Alignment.Center)
                    .animateEnterExit(
                        enter = slideInVertically(),
                        exit = slideOutVertically()
                    )
                    .sizeIn(minWidth = 256.dp, minHeight = 64.dp)
                    .background(Color.Magenta)
                ) {
                    Text(text = "显示")
                }
            }
        }
    }
}

@Preview
@Composable
fun MutableTransitionStateSimple() {
    val state = remember {
        MutableTransitionState(false).apply {
            targetState = true
        }
    }

    Column {
        AnimatedVisibility(visibleState = state) {
            Text(text = "Hello World")
        }

        Text(text = when {
            state.isIdle && state.currentState -> "Visible"
            !state.isIdle && state.currentState -> "Disappearing"
            state.isIdle && !state.currentState -> "Invisible"
            else -> "Appearing"
        })
    }
}

